import Vue from 'vue'
import createPersistedState from 'vuex-persistedstate'
import Vuex from 'vuex'
import store from '@/store'
import { getuserinfoAPI } from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    category: [],
    userinfo: {},
    storedetail: {},
    articledetail: {},
    // 结算订单商品
    orderList: [],
    // 结算金额
    price: '',
    // 订单列表
    myOrder: []
  },
  getters: {

  },
  mutations: {
    // 获取token
    getToken (state, val) {
      state.token = val
    },
    // 获取分类
    getCategory (state, val) {
      state.category = val
    },
    // 获取用户信息
    getuserinfo (state, val) {
      state.userinfo = val
    },
    // 清除token
    clearToken (state, val) {
      state.token = val
    },
    // 获取商品详情
    getstoredetail (state, val) {
      state.storedetail = val[0]
    },
    // 获取资讯详情
    getarticledetail (state, val) {
      state.articledetail = val
    },
    // 获取结算订单信息
    getOrderList (state, val) {
      state.orderList = val
    },
    getOrderPrice (state, val) {
      state.price = val
    },
    getMyorder (state, val) {
      state.myOrder.unshift(val)
    }
  },
  actions: {
    async GETUSERINFO () {
      const { data } = await getuserinfoAPI()
      if (data.status === 0) {
        store.commit('getuserinfo', data.data)
      }
    }
  },
  modules: {
  },
  plugins: [
    createPersistedState()
  ]
})
